package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 数据报表service
 */
@Service
@Slf4j
public class ReportServiceImpl implements ReportService {
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    /**
     * 饼状图
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> getSetmealReport() {
        return orderDao.findReportBySetmeal();
    }

    //查询运营数据统计
    @Override
    public Map<String, Object> getBusinessReportData() {
        Map<String, Object> map = new HashMap<>();
        //查询当前时间
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String weekTimesStart = sdf.format(getWeekTimesStart());
        String monthTimesStart = sdf.format(getMonthTimesStart());
        //报表日期
        String today = sdf.format(date);
        map.put("reportDate", today);
        //今日新增会员数
        Integer todayNewMember = memberDao.findTodayNewMember(today);
        map.put("todayNewMember", todayNewMember);
        //总会员数
        Integer totalMember = memberDao.findTotalMember();
        map.put("totalMember", totalMember);
        //本周新增会员数
        Integer thisWeekNewMember = memberDao.findThisWeekNewMember(weekTimesStart);
        map.put("thisWeekNewMember", thisWeekNewMember);
        //本月新增会员数
        Integer thisMonthNewMember = memberDao.findThisMonthNewMember(monthTimesStart);
        map.put("thisMonthNewMember", thisMonthNewMember);
        //今日订单数量
        Integer todayOrderNumber = orderDao.findTodayOrderNumber(today);
        map.put("todayOrderNumber", todayOrderNumber);
        //今日到诊数量
        Integer todayVisitsNumber = orderDao.findTodayVisitsNumber(today);
        map.put("todayVisitsNumber", todayVisitsNumber);
        //本周订单数量
        Integer thisWeekOrderNumber = orderDao.findThisWeekOrderNumber(weekTimesStart);
        map.put("thisWeekOrderNumber", thisWeekOrderNumber);
        //本周到诊数量
        Integer thisWeekVisitsNumber = orderDao.findThisWeekVisitsNumber(weekTimesStart);
        map.put("thisWeekVisitsNumber", thisWeekVisitsNumber);
        //本月订单数量
        Integer thisMonthOrderNumber = orderDao.findThisMonthOrderNumber(monthTimesStart);
        map.put("thisMonthOrderNumber", thisMonthOrderNumber);
        //本月到诊数量
        Integer thisMonthVisitsNumber = orderDao.findThisMonthVisitsNumber(monthTimesStart);
        map.put("thisMonthVisitsNumber", thisMonthVisitsNumber);
        //热门套餐查询
        List<Map> hotSetmeal = orderDao.findHotSetmeal();
        map.put("hotSetmeal", hotSetmeal);

        //返回查询数据
        return map;

    }

    // 获得本月第一天0点时间
    public static Date getMonthTimesStart() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        return cal.getTime();
    }

    // 获得本周一0点时间
    public static Date getWeekTimesStart() {
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return cal.getTime();
    }
}
